// pages/orderCenter/xlzlOrder/info.js
const app = getApp();
var topOpacity = 0;
var list = [];
import {
  multiArray,
  objectMultiArray
} from '../../../utils/pickerLinkage.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "寻麓之旅", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    // 姓名
    name: '',
    // 电话
    phone: '',
    //人数
    peopleNum: '请选择人数',
    peopleNumList: [{
      id: 0,
      name: '5'
    }, {
      id: 1,
      name: '6'
    }, {
      id: 2,
      name: '7'
    }, {
      id: 3,
      name: '8'
    }, {
      id: 4,
      name: '9'
    }, {
      id: 5,
      name: '10'
    }, {
      id: 6,
      name: '11'
    }, {
      id: 7,
      name: '12'
    }, {
      id: 8,
      name: '13'
    }, {
      id: 9,
      name: '14'
    }, {
      id: 10,
      name: '15'
    }, {
      id: 11,
      name: '16'
    }, {
      id: 12,
      name: '17'
    }, {
      id: 13,
      name: '18'
    }, {
      id: 14,
      name: '19'
    }, {
      id: 15,
      name: '20'
    }, {
      id: 16,
      name: '21'
    }, {
      id: 17,
      name: '22'
    }, {
      id: 18,
      name: '23'
    }, {
      id: 19,
      name: '24'
    }, {
      id: 20,
      name: '25'
    }, {
      id: 21,
      name: '26'
    }, {
      id: 22,
      name: '27'
    }, {
      id: 23,
      name: '28'
    }, {
      id: 24,
      name: '29'
    }, {
      id: 25,
      name: '30'
    }, {
      id: 26,
      name: '31'
    }, {
      id: 27,
      name: '32'
    }, {
      id: 28,
      name: '33'
    }, {
      id: 29,
      name: '34'
    }, {
      id: 30,
      name: '35'
    }, {
      id: 31,
      name: '36'
    }, {
      id: 32,
      name: '37'
    }, {
      id: 33,
      name: '38'
    }, {
      id: 34,
      name: '39'
    }, {
      id: 35,
      name: '40'
    }],
    // 公司
    company: '',
    //行业
    industry: '请选择行业',
    industryList: [{
      id: '0',
      name: '地产'
    }, {
      id: '1',
      name: '媒体'
    }, {
      id: '2',
      name: '设计'
    }, {
      id: '3',
      name: '其他'
    }],
    //部门
    department: '',
    //职务
    duties: '',
    multiIndex: [0, 0],
    multiArray: multiArray,
    objectMultiArray: objectMultiArray,
    qd: '请选择知晓途径',
    qdList: [{
        id: '0',
        name: '麓悦江城官网'
      }, {
        id: '1',
        name: '自媒体平台'
      },
      {
        id: '2',
        name: '同行推荐'
      },
      {
        id: '3',
        name: '再次到访'
      },
      {
        id: '4',
        name: '其他'
      }
    ],

    openid: '',
    shareTitle: '',
    shareImg: '',
    position: 0
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  // 姓名
  nameIpt(e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 电话
  phoneIpt(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //选择参加人数
  peopleNumChange(e) {
    let that = this;
    that.setData({
      peopleNum: that.data.peopleNumList[e.detail.value].name,
    })
  },
  //公司
  companyIpt(e) {
    this.setData({
      company: e.detail.value
    })
  },
  //行业
  industryChange(e) {
    let that = this;
    that.setData({
      industry: that.data.industryList[e.detail.value].name,
    })
  },
  //知晓渠道
  qdChange(e) {
    let that = this;
    that.setData({
      qd: that.data.qdList[e.detail.value].name,
    })
  },
  //部门
  departmentIpt(e) {
    this.setData({
      department: e.detail.value
    })
  },
  //职务
  dutiesIpt(e) {
    this.setData({
      duties: e.detail.value
    })
  },
  bindMultiPickerChange: function (e) {
    var that = this;
    that.setData({
      "multiIndex[0]": e.detail.value[0],
      "multiIndex[1]": e.detail.value[1]
    })
  },
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    switch (e.detail.column) {
      case 0:
        list = []
        for (var i = 0; i < that.data.objectMultiArray.length; i++) {
          if (that.data.objectMultiArray[i].parid == that.data.objectMultiArray[e.detail.value].regid) {
            list.push(that.data.objectMultiArray[i].regname)
          }
        }
        that.setData({
          "multiArray[1]": list,
          "multiIndex[0]": e.detail.value,
          "multiIndex[1]": 0
        })
    }
  },
  //提交信息
  tj() {
    let that = this;
    if (that.data.name.trim() === '') {
      app.showTip('请输入姓名');
    } else if (that.data.phone.trim() === '') {
      app.showTip('请输入电话');
    } else if (isNaN(that.data.phone) || that.data.phone.length !== 11) {
      app.showTip('请输入正确电话');
    } else if (that.data.peopleNum === '请选择人数') {
      app.showTip('请选择人数');
    } else if (that.data.company.trim() === '') {
      app.showTip('请输入公司');
    } else if (that.data.industry.trim() === '请选择行业') {
      app.showTip('请选择行业');
    } else if (that.data.department.trim() === '') {
      app.showTip('请输入部门');
    } else if (that.data.duties.trim() === '') {
      app.showTip('请输入职务');
    } else if (that.data.multiArray[0][that.data.multiIndex[0]] == "") {
      app.showTip('请选择城市');
    } else if (that.data.qd.trim() === '请选择知晓途径') {
      app.showTip('请选择知晓途径');
    } else {
      var orderInfo = wx.getStorageSync('orderInfo');
      var chooseObj = wx.getStorageSync('chooseObj');
      var chooseMinute = wx.getStorageSync('chooseMinute');
      wx.showLoading({
        title: '数据提交中...'
      });

      wx.request({
        url: that.data.baseUrl + 'addXlBmNewInfo.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          position: that.data.position,
          name: that.data.name.trim(),
          tel: that.data.phone.trim(),
          people_number: that.data.peopleNum,
          company: that.data.company.trim(),
          industry: that.data.industry.trim(),
          section: that.data.department.trim(),
          job: that.data.duties.trim(),
          city: that.data.multiArray[0][that.data.multiIndex[0]] + "-" + that.data.multiArray[1][that.data.multiIndex[1]],
          region: that.data.qd.trim(),
          chooseMinute: chooseMinute,
          orderInfo: JSON.stringify(orderInfo),
          chooseObj: JSON.stringify(chooseObj)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data.status == 1) {
            //后台请求成功
            //清空缓存
            wx.setStorageSync('chooseMinute', '');
            wx.setStorageSync('orderInfo', '');
            wx.setStorageSync('chooseObj', '');

            //判断是否需要支付金额
            if (res.data.is_header_succ == 0) {
              //跳转支付页面
              wx.redirectTo({
                url: '/pages/orderCenter/xlzlOrder/peer-payment/peer-payment?id=' + res.data.id
              });
            } else {
              //跳转成功页面
              wx.redirectTo({
                url: '/pages/orderCenter/xlzlOrder/success'
              });
            }
          } else {
            //后台请求失败
            app.showTip(res.data.msg);
            return;
          }
        },
        fail: function (res) {
          wx.hideLoading();
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    var position = options.position;
    if (isNaN(position) || parseInt(position) < 1) {
      app.showTip('抱歉,未获取到您选择的导览类型哦');
      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/orderCenter/xlzlOrder/menu'
        })
      }, 1500);
      return;
    } else {
      that.setData({
        position: position
      })
    }

    //更新选择的人数
    var orderInfo = wx.getStorageSync('orderInfo');
    var bm_people = orderInfo.bm_people;
    that.setData({
      peopleNum: bm_people
    });

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getXlInfoageInfo(res.data.openid, position);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getXlInfoageInfo(openid, position);
    }

    //调用分享
    that.getShareInfo();
  },


  //获取本页数据
  getXlInfoageInfo(openid, position) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getXlInfoageInfo.php',
      method: 'POST',
      data: {
        openid: openid,
        position: position
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/orderCenter/index'
            })
          }, 1200)
          return;
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo_order.php',
      method: 'POST',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/orderCenter/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})